package org.andao.commons.taglib.ui;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.andao.commons.taglib.BaseTagSupport;
import org.andao.core.utils.ExStringUtils;
import org.andao.core.utils.JsonUtils;


public class SelectCourseTag extends BaseTagSupport {

    private static final long serialVersionUID = 8929683243218426166L;

    private String id;
    private String name;
    private String showOnly;
    private String time;
    private String phase;
    private String day;
    private String value;
    private String courses;
 
    @Override
    public int doEndTag() throws JspException {
        StringBuilder sb = new StringBuilder("");
        if("Y".equalsIgnoreCase(showOnly)){
            sb.append("<select");
            if (name != null && !name.equals("")) {
                if (ExStringUtils.isEmpty(id))
                    sb.append(" id='" + name + "'");
                else
                    sb.append(" id='" + id + "'");
                sb.append(" name='" + name + "'");
            }
            
            sb.append(">");
            List<Object> courseList = JsonUtils.jsonToBean(courses);
            for(Object obj : courseList){
            	Map<String, String> course = (Map<String, String>)obj;
                if(value.equals(course.get("subjectId"))){
                    sb.append("<option value='{\"time\":\"" + time + "\",\"phase\":\"" + phase + "\",\"day\":\"" + day + "\",\"course\":\"" + course.get("subjectId") + "\"}' selected>" + course.get("subjectName") + "</option>");
                }else{
                    sb.append("<option value='{\"time\":\"" + time + "\",\"phase\":\"" + phase + "\",\"day\":\"" + day + "\",\"course\":\"" + course.get("subjectId") + "\"}'>" + course.get("subjectName") + "</option>");
                }
            }
            sb.append("</select>");
        }else if("N".equalsIgnoreCase(showOnly)){
            sb.append(value);
        }
       
        JspWriter writer = this.pageContext.getOut();
        try {
            writer.append(sb.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return EVAL_PAGE;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getPhase() {
        return phase;
    }

    public void setPhase(String phase) {
        this.phase = phase;
    }

    public String getDay() {
        return day;
    }

    public void setDay(String day) {
        this.day = day;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getShowOnly() {
        return showOnly;
    }

    public void setShowOnly(String showOnly) {
        this.showOnly = showOnly;
    }

	public String getCourses() {
		return courses;
	}

	public void setCourses(String courses) {
		this.courses = courses;
	}

    
}

class Entity {
    public String Key;
    public String Value;

    public Entity(String key, String value) {
        this.Key = key;
        this.Value = value;
    }
}
